/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.mes.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.mes.domain.MesMenu;
import me.zhengjie.modules.mes.service.MesMenuService;
import me.zhengjie.modules.mes.domain.vo.MesMenuQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.HashSet;
import java.util.List;

import me.zhengjie.modules.system.domain.Menu;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author mfc
* @date 2024-12-31
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "MesMenu管理")
@RequestMapping("/api/mesMenu")
public class MesMenuController {

    private final MesMenuService mesMenuService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('mesMenu:list')")
    public void exportMesMenu(HttpServletResponse response, MesMenuQueryCriteria criteria) throws IOException {
        mesMenuService.download(mesMenuService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询MesMenu")
    @ApiOperation("查询MesMenu")
    @PreAuthorize("@el.check('mesMenu:list')")
    public ResponseEntity<PageResult<MesMenu>> queryMesMenu(MesMenuQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(mesMenuService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增MesMenu")
    @ApiOperation("新增MesMenu")
    @PreAuthorize("@el.check('mesMenu:add')")
    public ResponseEntity<Object> createMesMenu(@Validated @RequestBody MesMenu resources){
        mesMenuService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改MesMenu")
    @ApiOperation("修改MesMenu")
    @PreAuthorize("@el.check('mesMenu:edit')")
    public ResponseEntity<Object> updateMesMenu(@Validated @RequestBody MesMenu resources){
        mesMenuService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除MesMenu")
    @ApiOperation("删除MesMenu")
    @PreAuthorize("@el.check('mesMenu:del')")
    public ResponseEntity<Object> deleteMesMenu(@RequestBody List<Long> ids) {
        mesMenuService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("根据菜单ID返回所有子节点")
    @GetMapping(value = "/getChildMenus")
    public ResponseEntity<Object> getChildMenus(@RequestParam Long pId){
        List<MesMenu> menuList = mesMenuService.getChildMenus(pId);
        return new ResponseEntity<>(menuList,HttpStatus.OK);
    }
}